﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyBBS.Services;
using MyBBS.Controllers.ViewData;
using MyBBS.Data.Models;
using MyBBS.Data.Utility;
using MyBBS.Data.Filters;

namespace MyBBS.Controllers
{
    public class PostController : BaseController
    {
        private IPostService _postService = null;

        public PostController(IPostService postService, IUserService userService) : base(userService)
        {
            this._postService = postService;

            if (_postService == null)
                throw new InvalidOperationException("Service should not be null");
        }


        public ActionResult Index(int? page)
        {
            PostListViewData data = new PostListViewData()
                    {
                        Categories = _postService.GetCategories(),
                        PostsUserInfo = new PostListUserInfoViewData{
                            PostList = Pagination.ToPagedList<Post>(_postService.GetPosts(), page),
                            IsCurrentUserAuthenticated = base.IsAuthenticated
                        },
                        //PostList = Pagination.ToPagedList<Post>(_postService.GetPosts(), page),
                        IsAuthenticated = base.IsAuthenticated,
                        UserName = base.UserName,
                        Tags = _postService.GetPopularTags(),
                    };

            data.PostsUserInfo.PostList.ForEach(            
                p => p.IsKicked = base.IsAuthenticated ? p.Kicks.Count( k => k.UserID == base.CurrentUserID) > 0 : false              
            );
            return View(data);
        }

        public ActionResult Category(long? id, int? page)
        {
            if (id == null)
                return RedirectToAction("Index");
            else
            {

                PostListByCategoryViewData postListByCategory = new PostListByCategoryViewData()
                        {
                            Categories = _postService.GetCategories(),
                            PostsUserInfo = new PostListUserInfoViewData
                            {
                                PostList = Pagination.ToPagedList<Post>(_postService.GetPosts(), page),
                                IsCurrentUserAuthenticated = base.IsAuthenticated
                            },
                            IsAuthenticated = base.IsAuthenticated,
                            UserName = base.UserName,
                            CategoryID = id.Value,
                            CategoryName = _postService.GetCategoryByID(id.Value).CategoryName,
                            Tags = _postService.GetPopularTags(),
                        };

                return View("PostByCategory", postListByCategory);
            }
        }

        public ActionResult Tag(long tagID, int? page)
        {
            PostListByTagViewData postListByTag = new PostListByTagViewData()
                {
                    Categories = _postService.GetCategories(),
                    IsAuthenticated = base.IsAuthenticated,
                    UserName = base.UserName,
                    Tags = _postService.GetPopularTags(),
                    TagID = tagID,
                    TagName = _postService.GetPopularTags().WithIDEquals(tagID).TagName,
                    PostsUserInfo = new PostListUserInfoViewData
                    {
                        PostList = Pagination.ToPagedList<Post>(_postService.GetPosts(), page),
                        IsCurrentUserAuthenticated = base.IsAuthenticated
                    },
                };

            return View("PostByTag", postListByTag);
        }

        public ActionResult Show(long postID, int? commentPage)
        { 
            PostViewData post = new PostViewData()
                    {
                        Categories = _postService.GetCategories(),
                        PostInfo = _postService.GetPostByID(postID),
                        IsAuthenticated = base.IsAuthenticated,
                        UserName = base.UserName,
                        Tags = _postService.GetPopularTags(),
                    };

            return View("Show", post);
        }


        public ActionResult New(string storyURL, string title, string description, long categoryID, string tags)
        {
            if (this.IsAuthenticated)
            {
                Post post = new Post() 
                    { 
                        URL = storyURL, 
                        Title = title, 
                        CategoryID = categoryID, 
                        Description = description, 
                        PostedTime = DateTime.Now, 
                        Author = this.UserName,
                        Tags = tags
                    };

                long postID = _postService.New(post);

                PostViewData newPost = new PostViewData();
                newPost.PostInfo = _postService.GetPostByID(postID);
                newPost.IsAuthenticated = base.IsAuthenticated;
                newPost.UserName = base.UserName;

                //return View("NewPostFormat", newPost);
            }
            else
            {
            }
            return null;
        }

        public ActionResult Kick(long postID)
        {
            KickResult kickResult = new KickResult();
            kickResult.KickCount = _postService.Kick(new Kick { PostID = postID, UserID = base.CurrentUserID });

            return View("Json", kickResult);
        }

        /// <summary>
        /// Submit a new comment
        /// </summary>
        /// <param name="postID">Post's ID</param>
        /// <param name="content">comment's content</param>
        /// <param name="author">comment's author</param>
        /// <returns></returns>
        public ActionResult SubmitComment(long postID, string content, string author)
        {

            long commentID = _postService.SubmitComment(new Comment() { PostID=postID, Content=content, Author=author});

            NewCommentResult comment = new NewCommentResult();
            Comment com = _postService.GetCommentByID(commentID);
            comment.IsOdd = (_postService.GetPostByID(postID).Comments.Count & 1) == 1;
            comment.Content = com.Content;
            comment.Author = com.Author;
            comment.SubmitTime = com.SubmitTime;

            return View("Json", comment);

        }
    }
}
